home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / net / netamsrc.arc / mac_io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-12  |  9.7 KB  |  531 lines

  1. /* OS- and machine-dependent stuff for Mac  */
  2.  
  3. /*mac_io.c
  4.  *    Routines for Macintosh IO and file stuff
  5.  */
  6. #include <stdio.h>
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "iface.h"
  11. #include "mac.h"
  12. #include "cmdparse.h"
  13. #include <ctype.h>
  14.  
  15. #include "DeviceMgr.h"
  16. #include "WindowMgr.h"
  17. #include "EventMgr.h"
  18. #include "SerialDvr.h"
  19. #include "HFS.h"
  20. #include <time.h>
  21. extern long timezone;
  22. extern errno;
  23.  
  24. struct Store_input Store_input[ASY_MAX];
  25.  
  26. struct asy asy[ASY_MAX];
  27.  
  28. static ioParam    MacSer;
  29.  
  30. struct interface *ifaces;
  31. struct RemoveIt Head;
  32.  
  33. unsigned nasy;
  34.  
  35. /* Called at startup time to set up console I/O, memory heap */
  36. ioinit()
  37. {
  38.     timezone = 60*60+1;
  39.     tzname[0] = "CST";
  40.     tzname[1] = "CST";
  41.     Click_On(0);
  42.     mac_files();
  43.     return(0);
  44. }
  45. /* Called just before exiting to restore console state */
  46. iostop()
  47. {
  48.     int i;
  49.     struct RemoveIt *rptr;
  50.     
  51.     while(ifaces != NULLIF){
  52.         if(ifaces->stop != NULLFP)
  53.             (*ifaces->stop)(ifaces);
  54.         ifaces = ifaces->next;
  55.     }
  56.     /*
  57.      * I want to close down all the files and then remove the (possibly still existing) files
  58.      * because the MAC will not allow the file to be removed when it still has an open file
  59.      * descriptor
  60.      */
  61.      
  62.     for( i = 3; i < _NFILE; i++)
  63.     {
  64.         close(i);
  65.     }
  66.     (void)unlink("dir.temp");
  67.     rptr = &Head;
  68.     while( rptr->next != NULL)
  69.     {
  70.         if ( rptr->name_ptr != NULL)
  71.         {
  72.             unlink(rptr->name_ptr);
  73.             free(rptr->name_ptr);
  74.         }
  75.         rptr = rptr->next;
  76.     }    
  77. }
  78.  
  79. /* Initialize asynch port "dev" */
  80.  
  81. int slipisopen = 0;
  82. int iref;
  83. int oref;
  84.  
  85. char Recv_buf[MAX_STORE];
  86. char Send_buf[MAX_STORE];
  87.  
  88. int
  89. asy_init(dev,bufsize)
  90. int16 dev;
  91. unsigned bufsize;
  92. {
  93.     register struct asy *ap;
  94.     register struct interface *if_asy;
  95.     extern struct interface *ifaces;
  96.     char ser_name[255];
  97.     struct Store_input *store;
  98.     
  99.     OsErr e;
  100.     ap = &asy[dev];
  101. #ifdef DEBUG
  102.     printf("asy_init: as->tty = %s, dev = %d\n", ap->tty, dev); 
  103. #endif
  104.     if ( ap == NULL)
  105.     {
  106.         return(-1);
  107.     }
  108.     /*
  109.      * the user can use either port 'a' or 'b'.  'A' is the perfered
  110.      */
  111.     
  112.     switch ( ap->tty[0] )
  113.     {
  114.         case 'a':
  115.         case 'A':
  116.             if ( ap->devopen == 0 )
  117.             {
  118.                 e = RAMSDOpen(sPortA);
  119.                 if ( e != noErr )
  120.                 {
  121.                     printf("RAMSDOpen failed, e = %d\n",e);
  122.                     SysBeep(4);
  123.                     ExitToShell();
  124.                 }
  125.                 e = OpenDriver ("\p.AIn", &iref);
  126.                 if ( e != noErr) {
  127.                     printf("OpenDriver for AIn failed, e = %d\n",e);
  128.                     SysBeep (4);
  129.                     ExitToShell ();
  130.                 }
  131.                 e = OpenDriver ("\p.AOut", &oref);
  132.                 if ( e != noErr) {
  133.                     printf("OpenDriver for AOut failed, e = %d\n",e);
  134.                     SysBeep (4);
  135.                     CloseDriver (iref);
  136.                     ExitToShell ();
  137.                 }
  138.                 ap->portIn = AinRefNum;
  139.                 ap->portOut = AoutRefNum;
  140.                 ap->devopen = 1;
  141.             }
  142.             else
  143.             {
  144.                 printf("Device %c is already open.\n", ap->tty[0]);
  145.                 return(-1);
  146.             }
  147.             break;
  148.         
  149.         case 'b':
  150.         case 'B':
  151.             if ( ap->devopen == 0)
  152.             {
  153.                 e = RAMSDOpen(sPortB);
  154.                 if ( e != noErr )
  155.                 {
  156.                     printf("RAMSDOpen failed, e = %d\n", e);
  157.                     SysBeep(4);
  158.                     ExitToShell();
  159.                 }
  160.                 e = OpenDriver ("\p.BIn", &iref);
  161.                 if ( e != noErr) {
  162.                     printf("OpenDriver for BIn failed, e = %d\n", e);
  163.                     SysBeep (4);
  164.                     ExitToShell ();
  165.                 }
  166.                 e = OpenDriver ("\p.BOut", &oref);
  167.                 if ( e != noErr) {
  168.                     printf("Opendriver for Bout failed e = %d\n",e);
  169.                     SysBeep (4);
  170.                         CloseDriver (iref);
  171.                     ExitToShell ();
  172.                 }
  173.                 ap->portIn = BinRefNum;
  174.                 ap->portOut = BoutRefNum;
  175.                 ap->devopen = 1;
  176.             }
  177.             else
  178.             {
  179.                 printf("Device %c is already open.\n", ap->tty[0] );
  180.                 return(-1);
  181.             }
  182.             break;
  183.         
  184.         default:
  185.             printf("Unknown device %c, could not configure port,\n", ap->tty);
  186.             return(-1);
  187.     }
  188.     
  189.     /* setup the ring buffer for input on this device */    
  190.         
  191.     store = &Store_input[dev];
  192.     store->head =  store->store;
  193.     store->tail = store->store;
  194.     store->amt = 0;
  195.     /*
  196.      * tell the device that it can receive only MAX_STORE
  197.      */
  198.     e = SerSetBuf( ap->portIn, Recv_buf, MAX_STORE);
  199.     if ( e != noErr) {
  200.         printf("asy_init: SerSetBuf error on %d\n", ap->portIn);
  201.         SysBeep (4);
  202.         CloseDriver (iref);
  203.         ExitToShell ();
  204.     }
  205.     SerSetBuf( ap->portOut, Send_buf, MAX_STORE);
  206.     if ( e != noErr) {
  207.         printf("asy_init: SerSetBuf error on %d\n", ap->portOut);
  208.         SysBeep (4);
  209.         CloseDriver (iref);
  210.         ExitToShell ();
  211.     }
  212.     slipisopen = 1;
  213.     return (0);
  214. }
  215.  
  216. int
  217. asy_stop(interface)
  218. struct interface *interface;
  219. {
  220.     register struct asy *ap;
  221.     int ref;
  222.     
  223.     ap = &asy[interface->dev];
  224.  
  225.     if (slipisopen)
  226.     {
  227.         /*
  228.          * kill off any io pending 
  229.          */
  230.         MacSer.ioActCount = 0;
  231.         MacSer.ioRefNum = ap->portIn;
  232.         MacSer.ioCompletion = 0;
  233.         MacSer.ioBuffer = ap->recv_buf;
  234.         MacSer.ioReqCount = 0;
  235.         MacSer.ioPosMode = 1;
  236.         (void) PBKillIO(&MacSer, FALSE);
  237.         
  238.         MacSer.ioActCount = 0;
  239.         MacSer.ioRefNum = ap->portOut;
  240.         MacSer.ioCompletion = 0;
  241.         MacSer.ioBuffer = ap->recv_buf;
  242.         MacSer.ioReqCount = 0;
  243.         MacSer.ioPosMode = 1;
  244.         (void) PBKillIO(&MacSer, FALSE);
  245.  
  246.         FSClose(ap->portIn);
  247.         FSClose(ap->portOut);
  248.         if ( ap->portIn = AinRefNum)
  249.         {
  250.             OpenDriver("\p.Ain", &ref);
  251.             OpenDriver("\p.Aout", &ref);
  252.         }
  253.         else
  254.         {
  255.             OpenDriver("\p.Bin", &ref);
  256.             OpenDriver("\p.Bout", &ref);
  257.         }
  258.         
  259.         slipisopen = 0;
  260.     }
  261. }
  262.  
  263. /* Asynchronous line I/O control */
  264. asy_ioctl(interface,argc,argv)
  265. struct interface *interface;
  266. int argc;
  267. char *argv[];
  268. {
  269.     if(argc < 1){
  270.         printf("%d\r\n",asy[interface->dev].speed);
  271.         return 0;
  272.     }
  273.     return asy_speed(interface->dev,atoi(argv[0]));
  274. }
  275.  
  276.  
  277. /* Set asynch line speed */
  278. int
  279. asy_speed(dev,speed)
  280. int dev;
  281. int speed;
  282. {
  283.     OsErr e;
  284.     int serialconfig;
  285.     SerShk    HandShake;
  286.     struct asy *ap;
  287.  
  288.     
  289.     ap = &asy[dev];
  290.  
  291.     if(speed == 0 || dev >= nasy)
  292.         return(-1);
  293.     
  294. #ifdef DEBUG
  295.     printf("asy_speed: Setting speed for device %d to %d\n",dev, speed);
  296. #endif
  297.     asy[dev].speed = speed;
  298.  
  299.     switch(speed)
  300.     {
  301.         case 300:
  302.             serialconfig = baud300;
  303.             break;
  304.         case 600:
  305.             serialconfig = baud600;
  306.             break;
  307.         case 1200:
  308.             serialconfig = baud1200;
  309.             break;
  310.         case 1800:
  311.             serialconfig = baud1800;
  312.             break;
  313.         case 2400:
  314.             serialconfig = baud2400;
  315.             break;
  316.         case 3600:
  317.             serialconfig = baud3600;
  318.             break;
  319.         case 4800:
  320.             serialconfig = baud4800;
  321.             break;
  322.         case 7200:
  323.             serialconfig = baud7200;
  324.             break;
  325.         case 9600:
  326.             serialconfig = baud9600;
  327.             break;
  328.         case 19200:
  329.             serialconfig = baud19200;
  330.             break;
  331.         case (long)57600:
  332.             serialconfig = baud57600;
  333.             break;
  334.         default:
  335.             printf("asy_speed: Unknown speed (%ld)\n", speed);
  336.             break;
  337.     }
  338. /*    
  339.     printf("serialconfig = %d\n", serialconfig);
  340. */
  341.     serialconfig |= (stop10|noParity|data8);
  342.     
  343.     /* not set the speed up */
  344.  
  345.     e = SerReset( ap->portIn, serialconfig);
  346.     if ( e != noErr) {
  347.         printf("asy_speed: could not set config for %d\n", ap->portIn);
  348.         SysBeep (4);
  349.         CloseDriver (iref);
  350.         ExitToShell ();
  351.     }
  352.     
  353.     bzero(&HandShake, sizeof (SerShk));
  354.     e = SerHShake(ap->portIn, &HandShake);
  355.     if ( e != noErr) {
  356.         printf("asy_speed: could not set handshake for %d\n", ap->portIn);
  357.         SysBeep (4);
  358.         CloseDriver (iref);
  359.         ExitToShell ();
  360.     }
  361.     
  362.     e = SerReset( ap->portOut, serialconfig);
  363.     if ( e != noErr) {
  364.         printf("asy_speed: could not set config for %d\n", ap->portOut);
  365.         SysBeep (4);
  366.         ExitToShell ();
  367.     }
  368.     bzero(&HandShake, sizeof (SerShk));
  369.     e = SerHShake(ap->portOut, &HandShake);
  370.     if ( e != noErr) {
  371.         printf("asy_speed: could not set handshake for %d\n", ap->portOut);
  372.         SysBeep (4);
  373.         CloseDriver (iref);
  374.         ExitToShell ();
  375.     }
  376.     /*
  377.     printf("asy_speed: completed.\n");
  378.     */
  379.     return(0);
  380.  
  381. }
  382. /* Send a buffer to serial transmitter */
  383. asy_output(dev,buf,cnt)
  384. unsigned dev;
  385. char *buf;
  386. unsigned short cnt;
  387. {
  388.     register struct asy *ap;
  389.     long amount = (long)cnt;
  390.     ap = &asy[dev];
  391.     /*
  392.     printf("asy_output called. dev = %x, cnt = %d\n", dev, cnt);
  393.     */
  394.     if(dev >= nasy)
  395.         return(-1);
  396.     FSWrite(ap->portOut,&amount, buf);
  397.     return(0);
  398.  
  399. }
  400.  
  401. /*
  402.  * Receive characters from asynch line
  403.  * Returns count of characters read
  404.  */
  405.  
  406. unsigned
  407. asy_recv(dev,buf,cnt)
  408. int dev;
  409. char *buf;
  410. unsigned cnt;
  411. {
  412.     long amount[8];
  413.     int tot = 0;
  414.     struct Store_input *store;
  415.     char store_it[MAX_STORE];
  416.     int got = 0;
  417.     int tt;
  418.      struct asy *ap;
  419.  
  420. #ifdef DEBUG
  421. printf("asy_recv: dev = %d\n", dev);    
  422. #endif
  423.  
  424.     if ( dev > nasy )
  425.     {
  426.         printf("Error on asy_recv. dev = %d, max = %d\n", dev, nasy);
  427.         return(-1);
  428.     }
  429.  
  430.     ap = &asy[dev];
  431.  
  432.     store = &Store_input[dev];        /* point to current */
  433.  
  434.     if ( cnt > 0 && store->amt > 0)
  435.     {
  436.         tt = min(cnt , store->amt);
  437.         tt = min(tt,    MAX_STORE);
  438.         got = 0;
  439.         
  440.         while ( tt-- > 0)
  441.         {
  442.             buf[got++] = *store->tail++;
  443.             if ( store->tail >= &store->store[MAX_STORE])
  444.             {
  445.                 store->tail = store->store;    /* wrap around */
  446.             }
  447.             if ( got >= cnt )
  448.                 break;
  449.         }
  450.         store->amt -= got;
  451.         return( got );
  452.     }
  453.     else if ( cnt == 0 )
  454.     {
  455.         return(0);
  456.     }
  457.     
  458.     /* 
  459.      * How much is waiting for us?  If any is waiting, this go and get it
  460.      */
  461.      
  462.     Status( ap->portIn, 2, &amount[0]);
  463.     if (  amount[0] > 0L) 
  464.     {
  465.         MacSer.ioRefNum = ap->portIn;
  466.         MacSer.ioCompletion = 0;
  467.         MacSer.ioBuffer = ap->recv_buf;
  468.         MacSer.ioReqCount = (amount[0] < MAX_STORE) ? amount[0] : MAX_STORE;
  469.         MacSer.ioPosMode = 1;
  470.         tot = PBRead(&MacSer, FALSE);
  471.         if ( MacSer.ioResult != noErr)
  472.         {
  473.             printf("asy_read: read failed.\n");
  474.         }
  475.         tt = MacSer.ioActCount;
  476.         got = 0;
  477.         while (tt-- > 0)
  478.         {
  479.             *store->head++ = ap->recv_buf[got++];
  480.             if ( store->head == &store->store[MAX_STORE] )
  481.             {
  482.                 /* printf("xx got wrap\n"); */
  483.                 store->head = store->store;
  484.             }
  485.             store->amt++;
  486.         }
  487.         if ( cnt > 0 && store->amt > 0)
  488.         {
  489.             tt = min(cnt , store->amt);
  490.             tt = min(tt,    MAX_STORE);
  491.             got = 0;
  492.         
  493.             /*
  494.              * now store it in the ring buffer
  495.              */
  496.             while ( tt-- > 0)
  497.             {
  498.                 buf[got++] = *store->tail++;
  499.                 if ( store->tail >= &store->store[MAX_STORE])
  500.                 {
  501.                     store->tail = store->store;    /* wrap around */
  502.                 }
  503.                 if ( got >= cnt )
  504.                     break;
  505.             }
  506.             store->amt -= got;
  507.             return( got );
  508.         }
  509.         return ( 0 );
  510.     }
  511.     return (got);
  512.  
  513. }
  514.  
  515. char *memchr(str, chr, cnt)
  516. char *str;
  517. char chr;
  518. int cnt;
  519. {
  520.     int i;
  521.     char *ptr;
  522.  
  523.     for( ptr = str, i = 0; i < cnt; i++, ptr++)
  524.     {
  525.         if (*ptr == chr )
  526.         {
  527.             return(ptr);
  528.         }
  529.     }
  530.     return(NULLCHAR);
  531. }